package ${package}.${subPackageName};

import java.util.Date;
import java.util.Map;
import javax.servlet.ServletRequest;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.starapp.core.web.Servlets;
import com.google.common.collect.Maps;
import ${package}.entity.${className};
import ${package}.service.${className}Service;

/**
 * ${className}管理的Controller, 使用Restful风格的Urls:
 * 

 <#if module?exists>
@RequestMapping(value = "/${module}/${classAttrName}")
 * List page     : GET /${module}/${classAttrName}/
 * Create page   : GET /${module}/${classAttrName}/create
 * Create action : POST /${module}/${classAttrName}/create
 * Update page   : GET /${module}/${classAttrName}/update/{id}
 * Update action : POST /${module}/${classAttrName}/update
 * Delete action : GET /${module}/${classAttrName}/delete/{id}
<#else>
 * List page     : GET /${classAttrName}/
 * Create page   : GET /${classAttrName}/create
 * Create action : POST /${classAttrName}/create
 * Update page   : GET /${classAttrName}/update/{id}
 * Update action : POST /${classAttrName}/update
 * Delete action : GET /${classAttrName}/delete/{id}
</#if>
 * 
 * @author ${author}
 */
@Controller
<#if module?exists>
@RequestMapping(value = "/${module}/${classAttrName}")
<#else>
@RequestMapping(value = "/${classAttrName}")
</#if>
public class ${className}Controller {

	private static final int PAGE_SIZE = 3;

	private static Map<String, String> sortTypes = Maps.newLinkedHashMap();
	static {
		sortTypes.put("auto", "自动");
		sortTypes.put("title", "标题");
	}

	@Autowired
	private ${className}Service ${classAttrName}Service;

	@RequestMapping(value = "")
	public String list(@RequestParam(value = "sortType", defaultValue = "auto") String sortType,
			@RequestParam(value = "page", defaultValue = "1") int pageNumber, Model model, ServletRequest request) {
		Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, "search_");

		Page<${className}> ${classAttrName} = ${classAttrName}Service.get${className}List( searchParams, pageNumber, PAGE_SIZE, sortType);

		model.addAttribute("${classAttrName}", ${classAttrName});
		model.addAttribute("sortType", sortType);
		model.addAttribute("sortTypes", sortTypes);
		// 将搜索条件编码成字符串，用于排序，分页的URL
		model.addAttribute("searchParams", Servlets.encodeParameterStringWithPrefix(searchParams, "search_"));

	<#if module?exists>
		return "${module}/${classAttrName}/${classAttrName}List";
	<#else>
		return "${classAttrName}/${classAttrName}List";
	</#if>
	}

	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String createForm(Model model) {
		model.addAttribute("${classAttrName}", new ${className}());
		model.addAttribute("action", "create");
	<#if module?exists>
		return "${module}/${classAttrName}/${classAttrName}Form";
	<#else>
		return "${classAttrName}/${classAttrName}Form";
	</#if>
	}

	@RequestMapping(value = "create", method = RequestMethod.POST)
	public String create(@Valid ${className} entity, RedirectAttributes redirectAttributes) {
		${classAttrName}Service.save${className}(entity);
		redirectAttributes.addFlashAttribute("message", "create success");
		
	<#if module?exists>
		return "redirect:/${module}/${classAttrName}/";
	<#else>
		return "redirect:/${classAttrName}/";
	</#if>
	}

	@RequestMapping(value = "update/{${firstPrimaryKey}}", method = RequestMethod.GET)
	public String updateForm(@PathVariable("${firstPrimaryKey}") ${firstPrimaryKeyType} ${firstPrimaryKey}, Model model) {
		model.addAttribute("${classAttrName}", ${classAttrName}Service.get${className}(${firstPrimaryKey}));
		model.addAttribute("action", "update");
	<#if module?exists>
		return "${module}/${classAttrName}/${classAttrName}Form";
	<#else>
		return "${classAttrName}/${classAttrName}Form";
	</#if>
	}

	@RequestMapping(value = "update", method = RequestMethod.POST)
	public String update(@Valid @ModelAttribute("preloadTask") ${className} entity, RedirectAttributes redirectAttributes) {
		${classAttrName}Service.save${className}(entity);
		redirectAttributes.addFlashAttribute("message", "update success");
	<#if module?exists>
		return "redirect:/${module}/${classAttrName}/";
	<#else>
		return "redirect:/${classAttrName}/";
	</#if>
		
	}

	@RequestMapping(value = "delete/{${firstPrimaryKey}}")
	public String delete(@PathVariable("${firstPrimaryKey}") ${firstPrimaryKeyType} ${firstPrimaryKey}, RedirectAttributes redirectAttributes) {
		${classAttrName}Service.delete${className}(${firstPrimaryKey});
		redirectAttributes.addFlashAttribute("message", "delete success");
	<#if module?exists>
		return "redirect:/${module}/${classAttrName}/";
	<#else>
		return "redirect:/${classAttrName}/";
	</#if>
		
	}

	/**
	 * 使用@ModelAttribute, 实现Struts2 Preparable二次部分绑定的效果,先根据form的id从数据库查出Task对象,再把Form提交的内容绑定到该对象上。
	 * 因为仅update()方法的form中有id属性，因此本方法在该方法中执行.
	 */
	@ModelAttribute("preloadTask")
	public ${className} get${className}(@RequestParam(value = "${firstPrimaryKey}", required = false) ${firstPrimaryKeyType} ${firstPrimaryKey}) {
		<#if firstPrimaryKeyType=="String">
		if (${firstPrimaryKey} != null) 
		<#else>
		if (${firstPrimaryKey} != null&&${firstPrimaryKey} != 0) 
		</#if>
		{
			return ${classAttrName}Service.get${className}(${firstPrimaryKey});
		}
		return null;
	}
}

